<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="backLogin">退出</el-button></el-header
    >
    <el-container>
      <el-aside :width="iscollapse?'64px':'200px'">
        <div class="collapse" @click="btnCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#049eff"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false" 
          router 
          :default-active="clickActive"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" :key="item.id" v-for="item of menulist">
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="activecolor('/'+item2.path)" :index="'/'+item2.path" :key="item2.id" v-for="item2 of item.children">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      menulist:[],
      iconsObj:{
        '125':'iconfont icon-users',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      iscollapse:false,
      clickActive:''
    }
  },
  methods: {
    backLogin() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMenuList(){
      const {data:res}=await this.$http.get('menus')
      // console.log(res);
      if(res.meta.status!==200) return this.$message.error(res.meta.msg)
      this.menulist=res.data
      // console.log(this.menulist);
    },
    btnCollapse(){
      this.iscollapse=!this.iscollapse
    },
    activecolor(path){
      this.clickActive=path
      window.sessionStorage.setItem('activepath',path)
    }
  },
  created(){
    this.getMenuList()
    this.clickActive=window.sessionStorage.getItem('activepath')
  }
};
</script>

<style scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 20px;
  color: white;
  font-size: 20px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.iconfont{
  padding-right: 8px;
}
.el-menu{
  border-right: 0;
}
.collapse{
  letter-spacing: 0.2em;
  line-height: 24px;
  background-color: #4a5064;
  text-align: center;
  cursor: pointer;
}
</style>